<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收支金额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入收支金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
                        v-model="queryParams.createDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="modifyBy">
        <el-input
          v-model="queryParams.modifyBy"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="modifyDate">
        <el-date-picker clearable
                        v-model="queryParams.modifyDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['finance:reconciliation:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['finance:reconciliation:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['finance:reconciliation:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-descriptions>
      <el-descriptions-item label="总利润">
        {{ profit }}
      </el-descriptions-item>
    </el-descriptions>

    <el-table v-loading="loading" :data="reconciliationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderCode"/>
      <el-table-column label="收支金额" align="center" prop="money"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="modifyBy"/>
      <el-table-column label="修改时间" align="center" prop="modifyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['finance:reconciliation:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收入支出核销对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-button
        size="mini"
        type="text"
        icon="el-icon-edit"
        @click="selOrder(1)"
      >选择采购订单
      </el-button>
      <el-button
        size="mini"
        type="text"
        icon="el-icon-edit"
        @click="selOrder(2)"
      >选择采购退货订单
      </el-button>
      <el-button
        size="mini"
        type="text"
        icon="el-icon-edit"
        @click="selOrder(3)"
      >选择销售订单
      </el-button>
      <el-button
        size="mini"
        type="text"
        icon="el-icon-edit"
        @click="selOrder(4)"
      >选择销售退货订单
      </el-button>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderCode">
          <el-input v-model="form.orderCode" disabled placeholder="请输入订单编号"/>
        </el-form-item>
        <el-form-item label="收支金额" prop="money">
          <el-input v-model="form.money" disabled placeholder="请输入收支金额"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--    选择订单-->
    <el-dialog :title="selTitle"
               :visible.sync="showSelOrder"
               width="60%"
               :before-close="handleClose"
               :modal-append-to-body="false"
               :append-to-body="true">
      <el-table :data="orderList">
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">
            {{ (queryOrderParams.pageNum - 1) * queryOrderParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="订单编号" align="center" prop="code">
          <template slot-scope="scope">
            <el-button type="text" @click="confirmOrder(scope.row)">{{
                scope.row.orderCode
              }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="金额" align="center" prop="money"/>
        <el-table-column label="创建人" align="center" prop="createBy"/>
        <el-table-column label="创建时间" align="center" prop="createDate"/>
      </el-table>
      <pagination
        v-show="totalOrder>0"
        :total="totalOrder"
        :page.sync="queryOrderParams.pageNum"
        :limit.sync="queryOrderParams.pageSize"
        @pagination="selOrder(selType)"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelSelOrder">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReconciliation,
  getReconciliation,
  delReconciliation,
  addReconciliation,
  updateReconciliation
} from "@/api/finance/reconciliation";
import request from "@/utils/request";

export default {
  name: "Reconciliation",
  data() {
    return {
      profit: 0,
      showSelOrder: false,
      orderList: [],
      selType: 0,
      //选择订单标题
      selTitle: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收入支出核销表格数据
      reconciliationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        money: null,
        createBy: null,
        createDate: null,
        modifyBy: null,
        modifyDate: null,
        isDel: null
      },
      queryOrderParams: {
        pageNum: 1,
        pageSize: 10,
      },
      totalOrder: 0,
      // 表单参数
      form: {
        orderCode: "",
        money: 0,
      },
    };
  },
  created() {
    this.getList();
    this.getProfit();
  },
  methods: {
    getProfit() {
      request({
        url: "/finance/reconciliation/getProfit",
        methods: "post",
      }).then(res => {
        this.profit = res.data;
      })
    },
    //确定订单
    confirmOrder(row) {
      this.form.orderCode = row.orderCode;
      this.form.money = row.money;
      this.showSelOrder = false;
    },
    //关闭选择
    cancelSelOrder() {
      this.showSelOrder = false;
    },
    //选择订单
    selOrder(selType) {
      this.selType = selType;
      this.getOrderList();
    },
    //获取订单列表
    getOrderList() {
      let selUrl = "";
      this.queryOrderParams.payStatus = 2;
      this.orderList = [];
      switch (this.selType) {
        case 1:
          this.selTitle = "选择 采购订单";
          selUrl = "/caigou/buyOrder/noReconList";
          break;
        case 2:
          this.selTitle = "选择 采购退货订单";
          this.queryOrderParams.payStatus = 3;
          selUrl = "/caigou/buyBackOrder/noReconList";
          break;
        case 3:
          this.selTitle = "选择 销售订单";
          selUrl = "/xiaoshou/order/noReconList";
          break;
        case 4:
          this.selTitle = "选择 销售退货订单";
          selUrl = "/xiaoshou/saleBack/noReconList";
          break;
        default:
          break;
      }
      request({
        url: selUrl,
        params: this.queryOrderParams,
        method: "get"
      }).then(res => {
        let resultList = res.rows;
        this.totalOrder = res.total;
        switch (this.selType) {
          case 1:
            //code totalAmount
            resultList.forEach(item => {
              const detail = {
                orderCode: item.code,
                money: -item.totalAmount,
                createBy: item.createBy,
                createDate: item.createTime
              };
              this.orderList.push(detail)
            });
            break;
          case 2:
            //buyBackOrderCode allInPrice
            resultList.forEach(item => {
              const detail = {
                orderCode: item.buyBackOrderCode,
                money: item.allInPrice,
                createBy: item.createBy,
                createDate: item.createDate
              };
              this.orderList.push(detail)
            });
            break;
          case 3:
            //code total
            resultList.forEach(item => {
              const detail = {
                orderCode: item.code,
                money: item.total,
                createBy: item.createBy,
                createDate: item.createDate
              };
              this.orderList.push(detail)
            });
            break;
          case 4:
            //saleBackCode total
            resultList.forEach(item => {
              const detail = {
                orderCode: item.saleBackCode,
                money: -item.total,
                createBy: item.createBy,
                createDate: item.createDate
              };
              this.orderList.push(detail)
            });
            break;
          default:
            break;
        }
        this.showSelOrder = true;
      })
    },
    /** 查询收入支出核销列表 */
    getList() {
      this.loading = true;
      listReconciliation(this.queryParams).then(response => {
        this.reconciliationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        money: null,
        createBy: null,
        createDate: null,
        modifyBy: null,
        modifyDate: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收入支出核销";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReconciliation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收入支出核销";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.orderCode == null || this.form.orderCode == '') {
        this.$notify({
          message: "请选择订单",
          type: "warning",
          title: "警告"
        });
        return;
      }
      this.form.createBy = this.$store.getters.name;
      this.form.createDate = new Date().getTime();
      addReconciliation(this.form).then(res => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
        this.getProfit();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除收入支出核销编号为"' + ids + '"的数据项？').then(function () {
        return delReconciliation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('finance/reconciliation/export', {
        ...this.queryParams
      }, `reconciliation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
